package com.thug.states;

import java.io.IOException;

import org.anddev.andengine.audio.sound.Sound;
import org.anddev.andengine.audio.sound.SoundFactory;
import org.anddev.andengine.engine.camera.ZoomCamera;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.util.Debug;

import android.content.Context;
import android.util.Log;

import com.thug.engine.SceneManager;
import com.thug.engine.SceneState;

public class MainMenuState extends SceneState {
	
	private boolean mFinished = false;
	
	private Sound mBtnSound;
	
	private TextureRegion maboutme_btnTexRegion;
	private TextureRegion maboutme_btn_downTexRegion;
	private TextureRegion mexit_btnTexRegion;
	private TextureRegion mexit_btn_downTexRegion;
	private TextureRegion mfindme_btnTexRegion;
	private TextureRegion mfindme_btn_downTexRegion;
	private TextureRegion mmenu_backgroundTexRegion;
	private TextureRegion mplay_btnTexRegion;
	private TextureRegion mplay_btn_downTexRegion;


	private Sprite maboutme_btnSprite;
	private Sprite maboutme_btn_downSprite;
	private Sprite mexit_btnSprite;
	private Sprite mexit_btn_downSprite;
	private Sprite mfindme_btnSprite;
	private Sprite mfindme_btn_downSprite;
	private Sprite mmenu_backgroundSprite;
	private Sprite mplay_btnSprite;
	private Sprite mplay_btn_downSprite;


	//private 

	public MainMenuState(SceneManager s) {
		super(s);
	}

	@Override
	public void onLoad() {
		
		loadSounds();
		mBtnSound = (Sound) getSceneMgr().getResource("menu_btn_sound");
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/menu/");
		Context context = getSceneMgr().getApplicationContext();
		
		BitmapTextureAtlas mMenuTexture= new BitmapTextureAtlas(512, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);

		maboutme_btnTexRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mMenuTexture, context, "aboutme_btn.png", 0, 321);
		maboutme_btn_downTexRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mMenuTexture, context, "aboutme_btn_down.png", 0, 345);
		mexit_btnTexRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mMenuTexture, context, "exit_btn.png", 0, 415);
		mexit_btn_downTexRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mMenuTexture, context, "exit_btn_down.png", 0, 439);
		mfindme_btnTexRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mMenuTexture, context, "findme_btn.png", 0, 369);
		mfindme_btn_downTexRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mMenuTexture, context, "findme_btn_down.png", 0, 392);
		mmenu_backgroundTexRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mMenuTexture, context, "menu_background.png", 0, 0);
		mplay_btnTexRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mMenuTexture, context, "play_btn.png", 0, 463);
		mplay_btn_downTexRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mMenuTexture, context, "play_btn_down.png", 157, 321);


		getSceneMgr().getEngine().getTextureManager().loadTexture(mMenuTexture);

		float xPos = 0, yPos = 0;
		maboutme_btnSprite = new Sprite(xPos, yPos, maboutme_btnTexRegion ) {
			@Override 
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) { 
				if(pSceneTouchEvent.isActionDown()) { 
					mBtnSound.play(); 
					maboutme_btn_downSprite.setVisible(true); 
				} 
				else if (pSceneTouchEvent.isActionUp()){ 
					maboutme_btn_downSprite.setVisible(false); 
				} 
				return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
			}
		};
		maboutme_btn_downSprite = new Sprite(xPos, yPos, maboutme_btn_downTexRegion );

		mexit_btnSprite = new Sprite(xPos, yPos, mexit_btnTexRegion ) {
			@Override 
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) { 
				if(pSceneTouchEvent.isActionDown()) { 
					mBtnSound.play(); 
					mexit_btn_downSprite.setVisible(true); 
				} 
				else if (pSceneTouchEvent.isActionUp()){ 
					mexit_btn_downSprite.setVisible(false);
					//getSceneMgr().finish();//.clearStates();
					mFinished = true;
				} 
				return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
			}
		};
		mexit_btn_downSprite = new Sprite(xPos, yPos, mexit_btn_downTexRegion );

		mfindme_btnSprite = new Sprite(xPos, yPos, mfindme_btnTexRegion ) {
			@Override 
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) { 
				if(pSceneTouchEvent.isActionDown()) { 
					mBtnSound.play(); 
					mfindme_btn_downSprite.setVisible(true); 
				} 
				else if (pSceneTouchEvent.isActionUp()){ 
					mfindme_btn_downSprite.setVisible(false);
					getSceneMgr().pushState(new FollowPlayerState(getSceneMgr()));
				} 
				return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
			}
		};
		mfindme_btn_downSprite = new Sprite(xPos, yPos, mfindme_btn_downTexRegion );

		mplay_btnSprite = new Sprite(xPos, yPos, mplay_btnTexRegion ) {
			@Override 
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) { 
				if(pSceneTouchEvent.isActionDown()) { 
					mBtnSound.play(); 
					mplay_btn_downSprite.setVisible(true); 
				} 
				else if (pSceneTouchEvent.isActionUp()){ 
					mplay_btn_downSprite.setVisible(false);
					getSceneMgr().pushState(new GamePlayState(getSceneMgr()));
				} 
				return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
			}
		};
		mplay_btn_downSprite = new Sprite(xPos, yPos, mplay_btn_downTexRegion );
		
		
		Scene mScene = this;
		
		mmenu_backgroundSprite = new Sprite(0, 0, mmenu_backgroundTexRegion);
		mScene.attachChild(mmenu_backgroundSprite);
		
		xPos = 10;
		yPos = 26;
		mScene.attachChild(mplay_btnSprite);
		mScene.attachChild(mplay_btn_downSprite);
		mScene.registerTouchArea(mplay_btnSprite);
		mplay_btn_downSprite.setVisible(false);
		mplay_btnSprite.setPosition(xPos, yPos);
		mplay_btn_downSprite.setPosition(xPos, yPos);
		
		
		xPos = 10;
		yPos = 88;
		mScene.attachChild(mfindme_btnSprite);
		mScene.attachChild(mfindme_btn_downSprite);
		mScene.registerTouchArea(mfindme_btnSprite);
		mfindme_btn_downSprite.setVisible(false);
		mfindme_btnSprite.setPosition(xPos, yPos);
		mfindme_btn_downSprite.setPosition(xPos, yPos);
		

		xPos = 10;
		yPos = 150;
		mScene.attachChild(maboutme_btnSprite);
		mScene.attachChild(maboutme_btn_downSprite);
		mScene.registerTouchArea(maboutme_btnSprite);
		maboutme_btn_downSprite.setVisible(false);
		maboutme_btnSprite.setPosition(xPos, yPos);
		maboutme_btn_downSprite.setPosition(xPos, yPos);


		xPos = 10;
		yPos = 216;
		mScene.attachChild(mexit_btnSprite);
		mScene.attachChild(mexit_btn_downSprite);
		mScene.registerTouchArea(mexit_btnSprite);
		mexit_btn_downSprite.setVisible(false);
		mexit_btnSprite.setPosition(xPos, yPos);
		mexit_btn_downSprite.setPosition(xPos, yPos);
		
		ZoomCamera camera = (ZoomCamera) getSceneMgr().getCamera();
		camera.setZoomFactor(2);
		float x = mmenu_backgroundSprite.getX() + mmenu_backgroundSprite.getWidth() * 0.5f ;
		float y = mmenu_backgroundSprite.getY() + mmenu_backgroundSprite.getHeight() * 0.5f;
		camera.setCenter(x, y);
		
	}

	@Override
	public void onDestroy() {
		Log.d("main menu state ", " destroyed called ");
		this.detachChildren();
	}
	
	@Override
	public void onResume() {
		ZoomCamera camera = (ZoomCamera) getSceneMgr().getCamera();
		camera.setZoomFactor(2);
		camera.setBoundsEnabled(false);
		float x = mmenu_backgroundSprite.getX() + mmenu_backgroundSprite.getWidth() * 0.5f ;
		float y = mmenu_backgroundSprite.getY() + mmenu_backgroundSprite.getHeight() * 0.5f;
		camera.setCenter(x, y);
		super.onResume();
	}
	
	
	@Override
	protected void onManagedUpdate(float pSecondsElapsed) {
		if(mFinished) {
			getSceneMgr().clearStates();
			getSceneMgr().finish();
		}
		Log.d("main menu state ", " reached here ");
		super.onManagedUpdate(pSecondsElapsed);
	}
	
	private void loadSounds() {
		SoundFactory.setAssetBasePath("mfx/");
        try {
               Sound menuSound = SoundFactory.createSoundFromAsset(getSceneMgr().getEngine().getSoundManager(), getSceneMgr().getApplicationContext(), "menu_click_sound.ogg");
               getSceneMgr().addResource("menu_btn_sound", menuSound);
        } catch (final IOException e) {
        	 Debug.e(e);
        }
	}
	

}
